/**
 * Notes: 报修管理
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2022-08-22  07:48:00 
 */

const BaseProjectAdminService = require('./base_project_admin_service.js');

const util = require('../../../../framework/utils/util.js');
const exportUtil = require('../../../../framework/utils/export_util.js');
const timeUtil = require('../../../../framework/utils/time_util.js');
const dataUtil = require('../../../../framework/utils/data_util.js');

const TaskModel = require('../../model/task_model.js');
const UserModel = require('../../model/user_model.js');

// 导出数据KEY
const EXPORT_TASK_DATA_KEY = 'EXPORT_TASK_DATA';

class AdminTaskService extends BaseProjectAdminService {


	/** 取得分页列表 */
	async getAdminTaskList({
		search, // 搜索条件
		sortType, // 搜索菜单
		sortVal, // 搜索菜单
		orderBy, // 排序
		whereEx, //附加查询条件 
		page,
		size,
		oldTotal = 0
	}) {

		orderBy = orderBy || {
			TASK_ADD_TIME: 'desc'
		};
		let fields = 'TASK_ID,TASK_STATUS,TASK_OBJ,TASK_ADD_TIME,TASK_LAST_TIME,TASK_USER,TASK_USER_ID';


		let where = {};
		where.and = {
			_pid: this.getProjectId(), //复杂的查询在此处标注PID  
		};

		if (util.isDefined(search) && search) {
			where.or = [
        { ['TASK_ID']: ['like', search] },
				{ ['TASK_OBJ.title']: ['like', search] },
				{ ['TASK_OBJ.person']: ['like', search] },
				{ ['TASK_OBJ.phone']: ['like', search] },
        { ['TASK_OBJ.sn']: ['like', search] },
        { ['TASK_OBJ.errorsn']: ['like', search] },
        { ['TASK_USER']: ['like', search] },
			];

		} else if (sortType && util.isDefined(sortVal)) {
			// 搜索菜单
			switch (sortType) {
				case 'vendor': {
					where.and['TASK_OBJ.vendor'] = sortVal;
					break;
        }
				case 'gpu': {
					where.and['TASK_OBJ.gpu'] = sortVal;
					break;
				}
				case 'status': {
					where.and['TASK_STATUS'] = Number(sortVal);
					break;
				}
				case 'sort': {
					orderBy = this.fmtOrderBySort(sortVal, 'TASK_ADD_TIME');
					break;
				}
			}
		}

		let result = await TaskModel.getList(where, fields, orderBy, page, size, true, oldTotal, false);


		// 为导出增加一个参数condition
		result.condition = encodeURIComponent(JSON.stringify(where));

		return result;
	}

	/**修改状态 */
	async statusTask(id, status, reason='', people='') {
    let where = {
			_id: id
		}
    let data = {
      TASK_STATUS: status,
      TASK_REASON: reason,
      TASK_PEOPLE: people
		}
		await TaskModel.edit(where, data);
	}

	// #####################导出数据

	/**获取数据 */
	async getTaskDataURL() {
		return await exportUtil.getExportDataURL(EXPORT_TASK_DATA_KEY);
	}

	/**删除数据 */
	async deleteTaskDataExcel() {
		return await exportUtil.deleteDataExcel(EXPORT_TASK_DATA_KEY);
	}

	/**导出数据 */
	async exportTaskDataExcel(forms) {
    let fields = 'TASK_ID,TASK_STATUS,TASK_OBJ,TASK_ADD_TIME,TASK_LAST_TIME,TASK_USER,TASK_ADD_IP,TASK_ADD_TIME,TASK_REASON,TASK_PEOPLE';
    let where = {};
		where.and = {
			_pid: this.getProjectId(), //复杂的查询在此处标注PID  
    };
    let orderBy = {
			TASK_ADD_TIME: 'desc'
    };
    let size = 1000;
    let result = await TaskModel.getAll(where, fields, orderBy, size, true);
    let status = ["待处理", "处理中", "已办结"];
    let data = [];
    let col = ["报修单号"];
    let i=0;
    let k=0;
    for (k = 0; k < forms.length; k++) {
      if (forms[k].type == 'content' || forms[k].type == 'image') continue;
      col.push(forms[k].title);
    }
    col.push("状态","IP","填报时间","结单描述","结单人","公司名称");
    data.push(col);
    let row = [];
    for(i=0;i<result.length;i++)
    {
        row.push(result[i].TASK_ID);
        for (k = 0; k < forms.length; k++) {
          if (forms[k].type == 'content' || forms[k].type == 'image') continue;
          row.push(result[i].TASK_OBJ[forms[k].mark]);
        }
        row.push(status[result[i].TASK_STATUS], result[i].TASK_ADD_IP, timeUtil.timestamp2Time(result[i].TASK_ADD_TIME), result[i].TASK_REASON, result[i].TASK_PEOPLE, result[i].TASK_USER);

        data.push(row);
        row = [];
    }

    return await exportUtil.exportDataExcel(EXPORT_TASK_DATA_KEY, '报修记录', data.length, data);
	}

}

module.exports = AdminTaskService;